﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Rendering;


/***********************************************
 * http://www.codeproject.com/Tips/757357/ASP-NET-MVC-apply-templates-to-enum-properties
 * mvc 5 不需要这个了
***********************************************/
//public class DataAnnotationsModelWithEnumMetadataProvider : DataAnnotationsModelMetadataProvider
//{
//    public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName)
//    {
//        var meta = base.GetMetadataForProperty(modelAccessor, containerType, propertyName);
//        if (meta.TemplateHint == null && typeof(Enum).IsAssignableFrom(meta.ModelType))
//        {
//            meta.TemplateHint = "Enum";//原理很简单，判断为枚举类型就使用枚举模版 模版未知
//        }
//        return meta;
//    }
//}

/*
 * 这种比较麻烦， http://odetocode.com/blogs/scott/archive/2012/09/04/working-with-enums-and-templates-in-asp-net-mvc.aspx
 * 但是更规范，有待研究
 */
//public class CustomMetadataProvider : DataAnnotationsModelMetadataProvider
//{
//    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
//    {
//        var mm = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
//        if (modelType.IsEnum && mm.TemplateHint == null)
//        {
//            mm.TemplateHint = "Enum";
//        }
//        return mm;
//    }
//}

namespace FwProject.Web.Mvc
{
    /// <summary>
    /// 枚举扩展
    /// </summary>
    public static class EnumExtension
    {
        //枚举值 本地化显示
        public static string DisplayName(this Enum value)
        {
            Type type = value.GetType();
            MemberInfo[] memInfo = type.GetMember(value.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((DisplayNameAttribute)attrs[0]).DisplayName;
            }
            return value.ToString();
            
        }



        /// <summary>  
        /// 根据枚举生成下拉列表的数据源  
        /// </summary>  
        /// <param name="enumType">枚举类型</param>  
        /// <param name="firstText">第一行文本(一般用于查询。例如：全部/请选择)</param>  
        /// <param name="firstValue">第一行值(一般用于查询。例如：全部/请选择的值)</param>  
        /// <returns></returns>  
        public static IList<SelectListItem> ToSelectList(Type enumType)
        {
            IList<SelectListItem> listItem = new List<SelectListItem>();
            
            if (enumType.IsEnum)
            {
                //AddFirst(listItem, firstText, firstValue);

                Array values = Enum.GetValues(enumType);
                if (null != values && values.Length > 0)
                {
                    foreach (var item in values)
                    {
                        listItem.Add(new SelectListItem { Value = item.ToString(), Text =item.ToString() });
                    }
                }
            }
            else
            {
                throw new ArgumentException("请传入正确的枚举！");
            }
            return listItem;
        }

        static void AddFirst(IList<SelectListItem> listItem, string firstText, string firstValue)
        {
            if (!string.IsNullOrWhiteSpace(firstText))
            {
                if (string.IsNullOrWhiteSpace(firstValue))
                    firstValue = "-1";
                listItem.Add(new SelectListItem { Text = firstText, Value = firstValue });
            }
        }

    }



}
